﻿using Hangjing.Common;
using Hangjing.Model;
using Hangjing.SQLServerDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Hangjing.WebCommon
{
    /// <summary>
    /// 此类主要用于多个站点都通用的方法
    /// </summary>
    public class WebHelper
    {
        /// <summary>
        /// 充值方式
        /// </summary>
        /// <param name="paytype"></param>
        /// <returns></returns>
        public static string Recharge(string paytype)
        {
            switch (paytype)
            {
                case "1":
                    return "支付宝";
                case "0":
                    return "后台转入";
                case "7":
                    return "订单结算";
                case "5":
                    return "微信";
                default:
                    return "";
            }
        }

        /// <summary>
        /// 美团订单加入正式系统
        /// </summary>
        /// <param name="orderid"></param>
        /// <returns></returns>
        public static int sendMTOrder2system(string orderid)
        {
            CustorderInfo infoaddress = new shopeleCustorder().GetModel(orderid);
            PointsInfo shop = new Points().GetModel(infoaddress.TogoId);

            ETogoLocalInfo info = new ETogoLocalInfo();
            ETogoLocal bll = new ETogoLocal();

            info = bll.GetInfoById(shop.Unid.ToString());

            infoaddress.tempcode = "";
            infoaddress.fromweb = ((int)OrderSource.mtapi).ToString();
            infoaddress.ReveVar1 = shop.sentorg;
            infoaddress.OldPrice = infoaddress.OrderSums;
            infoaddress.cityid = shop.cityid;
            infoaddress.TogoId = shop.Unid;
            infoaddress.paytime = Convert.ToDateTime("1970-1-1");
            infoaddress.IsShopSet = 1;
            infoaddress.ReveDate1 = DateTime.Now;
            infoaddress.Commission = WebHelper.getShopCommission(infoaddress.OrderSums, shop);
            infoaddress.settlecycle = shop.PType;
            infoaddress.OrderAddress = shop.Name;
            infoaddress.InUse = shop.PHead;
            infoaddress.deliverheaderid = shop.IsCallCenter;
            infoaddress.OldStatus = Convert.ToInt32(shop.PEnd);
            Custorder orderdal = new Custorder();

            apiResultInfo rs = new apiResultInfo();
            rs.state = 0;


            IList<Hangjing.Model.ETogoShoppingCart> Foods = new List<Hangjing.Model.ETogoShoppingCart>();

            Hangjing.Model.ETogoShoppingCart myshopcart = new Hangjing.Model.ETogoShoppingCart();
            myshopcart.TogoId = infoaddress.TogoId;
            myshopcart.sendfree = 0;
            //满多少元免配送费未处理
            myshopcart.ItemList = null;
            myshopcart.latlng = infoaddress.ReveVar2;

            IList<ETogoShoppingCartInfo> foodlist = new List<ETogoShoppingCartInfo>();

            IList<FoodlistInfo> orderfoods = new shopeleFoodlist().GetAllByOrderID(orderid);

            foreach (var item in orderfoods)
            {

                ETogoShoppingCartInfo cartfood = new ETogoShoppingCartInfo();
                cartfood.PId = item.FoodUnid;
                cartfood.PName = item.FoodName;
                cartfood.PNum = item.FCounts;
                cartfood.PPrice = item.FoodPrice;
                cartfood.Currentprice = cartfood.PPrice;
                cartfood.Foodcurrentprice = cartfood.PPrice;
                cartfood.Remark = "";

                foodlist.Add(cartfood);


            }
            myshopcart.ItemList = foodlist;
            Foods.Add(myshopcart);


            ////这里进行所有的判断
            IList<ROrderinfo> mylist = orderdal.SubmitOrder(Foods, infoaddress);

            if (mylist != null)
            {
                if (infoaddress.paystate == 1)
                {
                    orderdal.UpdatePayState(infoaddress);
                }

                Hangjing.DBUtility.SQLHelper.excutesql("UPDATE dbo.shopeleCustorder SET IsShopSet = 1,ReveDate1 = GETDATE() WHERE orderid = '" + orderid + "'");
            }
            else
            {
                return 0;
            }

            return 1;
        }




        /// <summary>
        /// 判断订单是否可以重新支付
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static bool CanPayAgain(CustorderInfo order)
        {
            bool payagian = false;
            if ((order.OrderStatus <= 2 || order.OrderStatus == 7) && order.paystate == 0 && (order.paymode == 1 || order.paymode == 5))
            {
                payagian = true;
            }

            return payagian;
        }

        /// <summary>
        /// 获取商家
        /// </summary>
        /// <param name="shopid"></param>
        /// <param name="PType">商家促销类型，-1表示没有，要在这里获取</param>
        /// <param name="PEnd">参与的平台促销项</param>
        /// <returns></returns>
        public static IList<webPromotionConfigInfo> getShopPromotions(int shopid, int PType, string PEnd)
        {
            IList<webPromotionConfigInfo> shoppromotions = new List<webPromotionConfigInfo>();
            IList<webPromotionConfigInfo> webPromotions = CacheHelper.GetWebPromotionConfig();

            if (PType < 0)
            {
                PointsInfo shop = new Points().GetModel(shopid);
                PType = shop.PType;
                PEnd = shop.PEnd;
            }

            switch (PType)
            {
                case 10:
                    {
                        shoppromotions = new webPromotionConfig().GetList(10, 1, "  startdate <= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' AND enddate >= '" + DateTime.Now.ToString("yyyy-MM-dd") + "' and isopen=1 and shopid = " + shopid, "pId", 1, 0);
                    }
                    break;
                case 20:
                    {
                        string[] promotionids = delBrackets(PEnd).Split(',');
                        foreach (var pid in promotionids)
                        {
                            foreach (var promotion in webPromotions)
                            {
                                if (pid.Trim() == promotion.pId.ToString())
                                {
                                    shoppromotions.Add(promotion);
                                }

                            }
                        }
                    }
                    break;
                default:
                    break;
            }


            return shoppromotions;
        }

        /// <summary>
        /// 传入{1},{2}..的字符串,过滤其中的括号
        /// </summary>
        /// <param name="mycbList"></param>
        /// <returns></returns>
        public static string delBrackets(string mycbList)
        {
            string tempcat = mycbList.Replace("{", "").Replace("}", "");
            return tempcat;
        }


        /// <summary>
        /// 根据订单信息，计算配 应交配送费，利润
        /// </summary>
        /// <param name="order"></param>
        public static void setSettleData( CustorderInfo order)
        {
            order.tempcode = "0";
            order.delivergivemoney = 0;
            if (order.settlecycle == 1 || order.SystemUserId == 1)
            {
                order.delivergivemoney = 0;
            }
            else
            {
                order.delivergivemoney = order.shopdiscountmoney;
            }
        }

        /// <summary>
        /// 获取商家的佣金
        /// </summary>
        /// <param name="foodprice"></param>
        /// <param name="shop"></param>
        /// <returns></returns>
        public static decimal getShopCommission(decimal foodprice, PointsInfo shop)
        {
            decimal commission = 0;
            switch (shop.Sn2Key)
            {
                case "0":
                    commission = foodprice * Convert.ToDecimal((Convert.ToDouble(shop.SendFee) / 100));
                    break;
                case "1":
                    commission = shop.SendFee;
                    break;
            }


            return commission;
        }

        /// <summary>
        /// 根据月份创建文件文件夹
        /// </summary>
        public static string CreateDirectoryByMonth(HttpContext context)
        {
            string strDay = System.DateTime.Now.ToString("yyyyMM");
            string DirUrl = "~/upload/" + strDay + "/";
            if (!System.IO.Directory.Exists(context.Server.MapPath(DirUrl)))//检测文件夹是否存在，不存在则创建
            {
                System.IO.Directory.CreateDirectory(context.Server.MapPath(DirUrl));
            }
            return DirUrl;

        }

        /// <summary>
        /// 去第一个的符号
        /// </summary>
        /// <param name="old"></param>
        /// <returns></returns>
        public static string delfirst(string old)
        {
            return System.Text.RegularExpressions.Regex.Replace(old, @"^,", "");
        }

        /// <summary>
        /// 订单接受状态（商家）
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnDeliverAuth(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "<font color='red'>未审核</font>"; break;
                case 1:
                    ret = "已审核"; break;
                case 2:
                    ret = "拒绝"; break;
            }

            return ret;
        }

        /// <summary>
        /// 订单接受状态（商家）
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnOrderReceiveState(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "未接"; break;
                case 1:
                    ret = "已接"; break;
                case 2:
                    ret = "拒绝"; break;
            }

            return ret;
        }

        /// <summary>
        /// 提现状态
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnCashOutState(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "未处理"; break;
                case 1:
                    ret = "已打款"; break;
                case 2:
                    ret = "管理员拒绝"; break;
                case 3:
                    ret = "商家取消"; break;
            }

            return ret;
        }

        /// <summary>
        /// 工作状态
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnWorkstate(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "下班"; break;
                case 10:
                    ret = "待岗"; break;
                case 20:
                    ret = "取货中"; break;
                case 30:
                    ret = "送货中"; break;
                case 40:
                    ret = "异常"; break;
            }

            return ret;
        }

        /// <summary>
        /// 岗位
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string Turntation(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "配送员"; break;
                case 10:
                    ret = "组长"; break;
                case 20:
                    ret = "副主管"; break;
                case 30:
                    ret = "主管"; break;
                case 40:
                    ret = "副经理"; break;
                case 50:
                    ret = "经理"; break;
            }

            return ret;
        }

        /// <summary>
        /// 商家配送费计算类型
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string shopSendFeeType(object State,object point)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 0:
                    ret = "每单提成 <font color='red'>"+point+"%</font>"; break;
                case 1:
                    ret = "按单收费 <font color='red'>" + point + "</font>元"; break;
                case 2:
                    ret = "按距离"; break;

            }

            return ret;
        }

        /// <summary>
        /// 职位
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnPost(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 100:
                    ret = "全职"; break;
                case 90:
                    ret = "兼职"; break;

            }

            return ret;
        }

        /// <summary>
        /// 车辆所有
        /// </summary>
        /// <param name="State"></param>
        /// <returns></returns>
        public static string TurnCarOwner(object State)
        {
            string ret = "";
            switch (Convert.ToInt32(State))
            {
                case 1:
                    ret = "公司"; break;
                case 0:
                    ret = "自带"; break;

            }

            return ret;
        }

        /// <summary>
        /// 判断当前优惠券编号是否可用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static resultinfo checkCard(string cardckey, decimal curmoney)
        {
            resultinfo rs = new resultinfo();
            rs.status = 0;
            rs.data = "";

            ShopCard dal = new ShopCard();
            string sql = "  ckey = '" + cardckey + "'";
            IList<ShopCardInfo> cardlist = dal.GetList(1, 1, sql, "cid", 1);
            if (cardlist.Count == 0)
            {
                rs.status = 20; ////优惠券券号错误，请重新输入
                return rs;
            }
            else
            {
                if (cardlist[0].Inve2 == "0")
                {
                    rs.status = 21; //这张券未激活，不能使用
                    return rs;

                }
                else
                {
                    if (cardlist[0].isused == 1)
                    {
                        rs.status = 22; //这张券已经使用过了，不能重复使用
                        return rs;
                    }
                    else
                    {
                        if (cardlist[0].moneyline > curmoney)
                        {
                            rs.status = 23; //此券商品100元可用
                            rs.moneyline = cardlist[0].moneyline.ToString("0");
                            return rs;
                        }
                        else
                        {
                            //可用，生成json
                            rs.status = 0;
                            decimal cmoney = 0;
                            switch (cardlist[0].ReveInt1)
                            {
                                case 1:
                                    cmoney = cardlist[0].Point;
                                    break;
                                case 2:
                                    cmoney = cardlist[0].Point;
                                    cmoney = curmoney * (100 - cmoney) / 100;
                                    break;
                                case 3:
                                    cmoney = Convert.ToInt32(cardlist[0].Point);
                                    break;
                                default:
                                    break;
                            }

                            rs.data = "[{'CID':'" + cardlist[0].CID + "','Point':'" + cmoney.ToString("0.0") + "','ReveInt1':'" + cardlist[0].ReveInt1 + "','ckey':'" + cardlist[0].ckey + "'}]";
                            return rs;
                        }

                    }
                }
            }

        }
    }

}
